﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventManager
{
    static EventManager ms_handle = new EventManager();
    Dictionary<int, System.Action<int,object>> mEventsDic = new Dictionary<int, System.Action<int, object>>(128);
    public static EventManager Instance
    {
        get
        {
            return ms_handle;
        }
    }

    public void SendEvent(int msgId,object argv)
    {
        if(mEventsDic.TryGetValue(msgId,out System.Action<int,object> handler) && null != handler)
        {
            handler.Invoke(msgId, argv);
        }
    }

    public void AddEvent(int msgId, System.Action<int, object> action)
    {
        if(mEventsDic.ContainsKey(msgId))
        {
            mEventsDic[msgId] += action;
        }
        else
        {
            mEventsDic.Add(msgId, action);
        }
    }

    public void RemoveEvent(int msgId, System.Action<int, object> action)
    {
        if(mEventsDic.TryGetValue(msgId,out System.Action<int, object> eventList))
        {
            mEventsDic[msgId] = System.Delegate.Remove(eventList, action) as System.Action<int, object>;
        }
    }
}